/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yayohos.locationmanager.base.slice;

import com.yayohos.locationmanager.LocationManager;
import com.yayohos.locationmanager.ResourceTable;
import com.yayohos.locationmanager.configuration.LocationConfiguration;
import com.yayohos.locationmanager.listener.LocationListener;
import com.yayohos.locationmanager.providers.locationprovider.DefaultLocationProvider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.location.Location;

/**
 * @noinspection checkstyle:Indentation
 * @date 2021/5/26
 */
public abstract class LocationBaseAbilitySlice extends AbilitySlice implements LocationListener {
    private LocationManager locationManager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_location_base_slice);
        locationManager = new LocationManager.Builder(getApplicationContext())
                .configuration(getLocationConfiguration())
                .locationProvider(new DefaultLocationProvider())
                .abilitySlice(this)
                .notify(this)
                .build();
    }

    /**
     * 获取 LocationConfiguration
     *
     * @return LocationConfiguration
     */
    public abstract LocationConfiguration getLocationConfiguration();

    /**
     * 获取 LocationManager
     *
     * @return LocationManager
     */
    protected LocationManager getLocationManager() {
        return locationManager;
    }

    /**
     * 获取Location
     */
    protected void getLocation() {
        if (locationManager != null) {
            locationManager.get();
        } else {
            throw new IllegalStateException("locationManager is null. "
                    + "Make sure you call super.initialize before attempting to getLocation");
        }
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
        locationManager.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        locationManager.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        locationManager.onStop();
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        locationManager.onAbilityResult(requestCode, resultCode, resultData);
    }


    @Override
    public void onProcessTypeChanged(int processType) {
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onLocationFailed(int type) {
    }

    @Override
    public void onPermissionGranted(boolean alreadyHadPermission) {
    }

    @Override
    public void onStatusChanged(int status) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    /**
     * AbilitySlice权限回调接口
     */
    public interface AbilitySliceCallBack {
        /**
         * 权限回调
         *
         * @param requestCode  requestCode
         * @param permissions  permissions
         * @param grantResults grantResults
         */
        void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults);
    }
}
